我有一个插件,有两种自定义的帖子类型-项目和任务。在插件中,您可以将任务分配给项目。例如,你可能有一个“网站设计”项目,任务是“购买域名”、“自定义主题”、“添加内容”等。
我要做的是创建一个项目列表,并在每个项目下面创建一个嵌套的任务列表,如下所示。
项目1任务1任务2在HTML中,我知道这是可行的:
<ul>
<li>Project 1
<ul>
<li>Task 1</li>
<li>Task 2</li>
</ul>
</li>
<li>Project 2
<ul>
<li>Task 3</li>
<li>Task 4</li>
</ul>
</li>
</ul>
我的插件在两个单独的函数中为项目和任务生成列表。我通过使用将任务包括在项目列表中
do_action( \'my-task-action\', $project_id )
Display projects
$project_option .= \'<ul class="project-list">\';
foreach( $projects as $project ) {
$project_id = $project->ID;
$project_option .= \'
<li class="project" id="project-\' . $project_id . \'">
<span class="project-title">\' . get_the_title( $project_id ) . \' </span>
<span class="project-status-title">\' . __( \'Project Status: \', \'client-area\' ) . \'</span>
<span class="project-status">\' . get_post_meta( $project_id, \'project-status\', true ) . \'</span>\' .
do_action( \'my-task-action\', $project_id ) . \'
</li>\';
}
$project_option .= \'</ul>\';
echo $project_option;
Display Tasks
显示任务的函数通过
do_action( \'my-task-action\', $project_id )
, 并可以显示分配给每个项目的每个任务。
$task_option .= \'<ul class="task-list">\';
foreach( $tasks as $task ) {
$task_id = $task->ID;
$task_option .= \'
<li class="task project-\' . $project_id . \'" id="task-\' . $task_id . \'">
<span class="task-title">\' . get_the_title( $task_id ) . \' </span>
<span class="task-status-title">\' . __( \'Task Status: \', \'client-area\' ) . \'</span>
<span class="task-status">\' . get_post_meta( $task_id, \'task-status\', true ) . \'</span>
</li>\';
}
$task_option .= \'</ul>\';
}
echo $task_option;
The Problem
我将任务放在一个列表中,项目放在一个单独的列表中,而不是像上面显示的那样嵌套列表。
任务1任务2任务3任务4
项目1
项目2或。。。
<ul class="task-list">
<li class="task project-1023" id="task-929">
<span class="task-title">Task 1 </span>
<span class="task-status-title">Task Status: </span>
<span class="task-status">on-hold</span>
</li>
<li class="task project-1023" id="task-927">
<span class="task-title">Task 2 </span>
<span class="task-status-title">Task Status: </span>
<span class="task-status">not-started</span>
</li>
<li class="task project-930" id="task-931">
<span class="task-title">Task 3 </span>
<span class="task-status-title">Task Status: </span>
<span class="task-status">on-hold</span>
</li>
<li class="task project-930" id="task-932">
<span class="task-title">Task 4 </span>
<span class="task-status-title">Task Status: </span>
<span class="task-status">not-started</span>
</li>
</ul>
<ul class="project-list">
<li class="project" id="project-1023">
<span class="project-title">Project 1 </span>
<span class="project-status-title">Project Status: </span>
<span class="project-status">in-progress</span>
</li>
<li class="project" id="project-930">
<span class="project-title">Project 2 </span>
<span class="project-status-title">Project Status: </span>
<span class="project-status">complete</span>
</li>
</ul>
根据do\\u操作在列表中的位置,我认为任务列表只会在项目的
<li></li>
, 并使用嵌套列表。
你知道为什么我没能如愿吗?
最合适的回答,由SO网友:czerspalace 整理而成
如果要在挂钩中返回值,请使用apply_filters
而不是do_action
, 例如:
$project_option .= \'<ul class="project-list">\';
foreach( $projects as $project ) {
$project_id = $project->ID;
$project_option .= \'
<li class="project" id="project-\' . $project_id . \'">
<span class="project-title">\' . get_the_title( $project_id ) . \' </span>
<span class="project-status-title">\' . __( \'Project Status: \', \'client-area\' ) . \'</span>
<span class="project-status">\' . get_post_meta( $project_id, \'project-status\', true ) . \'</span>\' .
apply_filters( \'my-task-action\', $project_id ) . \'
</li>\';
}
$project_option .= \'</ul>\';
echo $project_option;
然后有一个
add_filter
喜欢
add_filter(\'my-task-action\', \'wpse241018_getTaskActions\');
function wpse241018_getTaskActions($project_id){
$task_option .= \'<ul class="task-list">\';
foreach( $tasks as $task ) {
$task_id = $task->ID;
$task_option .= \'
<li class="task project-\' . $project_id . \'" id="task-\' . $task_id . \'">
<span class="task-title">\' . get_the_title( $task_id ) . \' </span>
<span class="task-status-title">\' . __( \'Task Status: \', \'client-area\' ) . \'</span>
<span class="task-status">\' . get_post_meta( $task_id, \'task-status\', true ) . \'</span>
</li>\';
}
$task_option .= \'</ul>\';
}
return $task_option;
}