Adding tags to WordPress pages
I try to display both posts and pages that are tagged with the same tag when I'm on a WordPress tag page. I'm using some custom code to be able to add the tag functionality to WordPress pages. Adding a tag to a page works and I can also display the tag on the page when I'm using <?php the_tags(); /> in a page template.
I'm using this code (in my child theme functions.php) to register the tag functionality to pages:
// Add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
add_action('init', 'tags_support_all');
The issue - Pages with tags are not being displayed on the tag page
I can't seem to find a way to get these pages displayed on the tag page. It only shows the posts that are tagged with the same tag. No pages. I use the code below to try and update the pre_get_posts query by setting the post_type to 'any'. It's an (old) snippet that I found on Google why I was searching for a solution. I'm not sure if this is still the way to go, but I couldn't find any working alternatives.
// Display all post types on tag pages
function tags_support_query($wp_query) {
if ($wp_query->get('tag')) {
$wp_query->set('post_type', 'any');
}
}
add_action('pre_get_posts', 'tags_support_query');
Any ideas on how to get this working?
Some extra information:
WordPress version 5.7.2.
I'm using a theme and (custom) child theme combination. The theme uses the Gutenberg editor.
The above code snippets are added to the child theme functions.php
There's no specific tag.php or archive.php. The tag page uses the default index.php to display the tag page. This index.php is also used on categories, archives, etc.
Create a new file that you can use for all Tags and call it tag.php. This will allow all Tag related archive pages to be rendered by this file. Other categories and archives will remain untouched.
In this new file copy all contents of index.php but add the following code in the top of the file.
/**
* Get the Term ID of the current term.
*/
$term = get_queried_object();
$term_id = $term->term_id;
/**
* Get all posts and pages with the current term.
*/
$args = array(
'post_type' => array('post', 'page'),
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => [$term_id]
)
)
);
/**
* Create a new query.
*/
$query = new WP_Query($args);
Then replace all occurences where the following occurs:
if ( have_posts() ) {
// replace with
if ( $query->have_posts() ) {
,
while ( have_posts() ) {
// replace with
while ( $query->have_posts() ) {
and
the_post();
// replace with
$query->the_post();
Then after the closing bracket of the while loop, add wp_reset_postdata(); to ensure that the post data related to the page has been reset. (otherwise the latest post in the loop will be considered the post of the page).
This should (at least) give you access to the posts and pages that have the post_tag term that corresponds with the page.
I am re-building our Learning Management System using WordPress. We use bootstrap and navwalker for the upper (main) navigation. I'd like to have another upper (main) navigation when the student is logged in. How would I go about this? Woudl you recommend my developer creates a different PHP Header Request?
So, at the moment all templates have this function:
<?php get_header();
Woudl the sensible thing be to create all the templates that need a different navigation like this:
<?php get_header(logged-in-templates);
So, the 'logged-in-templates) pull in the different navwalker...
Hope that makes sense!
Thanks
As i understood you want to display different nav menus if user is logged in and if the user is not logged in. In such case you can register additional nav menu location in functions.php:
register_nav_menus( array(
'logged_in_menu' => esc_html__( 'Menu for logged in users', 'yourthemename' ),
) );
and then just check
if(is_user_logged_in()) {
wp_nav_menu( array( 'logged_in_menu' => 'yourthemename' ) );
} else {
wp_nav_menu( array( 'primary_menu' => 'yourthemename' ) );
}
Is this what you are looking for?
I'm building a WordPress site and I need to create a menu made of child pages inside a parent page. So, I've achieved this correctly in 2 ways:
Building a custom menu and adding it inside a widget
Following this tutorial:
http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/
What I'm trying to get is smth similar as in the left side of this site: http://optinmonster.com/how-it-works/actionable-insights/
But I'm having a problem, cause when I click on the menu elements I created, they open up as pages of their own. While I want them to show up on the parent page... The idea is that I want different content to display in the parent page based on the menu items I click on. What should I do?? Is there a plugin for this??
P.S, I believe that I haven't yet found a solution for this problem because I'm googling it wrong, so is there a definite name for the problem I'm having??
I would really appreciate some help.
<?php
if ($post->post_parent) {
$page = $post->post_parent;
} else {
$page = $post->ID;
}
$children = wp_list_pages(array(
'child_of' => $page,
'echo' => '0',
'title_li' => ''
));
if ($children) {
echo "<ul>\n".$children."</ul>\n";
}
?>
in my admin page i can change the default template page to another custom template.
What i need is:
In my nav header menu i have many links and i have to set title attribute of a tag when the a tag point to a page that will be rendered with my custom template and not with the default one. Example:
<li><a hreh=".." title="myCustom">link1</a></li> //this title will be redirected with my custom template
<li><a hreh="..">link2</a></li> //this title will be redirected with default template
<li><a hreh="..">link3</a></li> //this title will be redirected with default template
<li><a hreh=".." title="myCustom">link4</a></li> //this title will be redirected with my custom template
If i open header.php that links are created by:
<?php $params = array(
'theme_location' =>'primary',
'limit' => 5,
'format' => 'custom',
'link_before' => '<span>',
'link_after' => '</span>' );
wp_nav_menu($params);
?>
How can i check if link is rendered by default template or by mine one?
I'm not sure what you are trying to do but about the title thing, you can easily add that at backend... Appearance > Menus. A sample image below..
I'm not sure if this will help but have you tried reading Creating Your Own Page Templates?
So that when you create a page, you can choose what template to use for that certain page. Like this image below,
If your theme is using the function body_class, like <body <?php body_class(); ?>>, your Html is already printing what you need to target elements only in that page:
This page is using the template file: /twentytwelve/page-templates/front-page.php.
You see there's a StackOverflow class there ;), and that's by:
add_filter('body_class', 'body_class_so_14302462');
function body_class_so_14302462( $classes )
{
global $post;
if( 'page' != $post->post_type )
return $classes;
$classes[] = 'STACKOVERFLOW';
return $classes;
}
Instead of adding a class to the array, you could completely override it using $classes = 'custom-and-only-class';, and also do all kind of checks to fine tune the output.
Is there any way to disable the option of adding a new post under a Custom Post Type in WordPress (3.0)? I've looked into labels and arguments but can't find anything that would resemble such a feature.
There is a meta capability create_posts that is documented here and is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
You'll probably want to set map_meta_cap to true as well. Without it, you won't be able to access the posts' editing pages anymore.
The combinations of the solutions above work in hiding the links (although someone could quite easily type the URL in directly.
The solution mentioned #3pepe3 relies on get_post_type() which will only work if there is already a post in the listing. If there are no posts, the function will not return anything, and the "Add New" link will be available. An alternative method:
function disable_new_posts() {
// Hide sidebar link
global $submenu;
unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);
// Hide link on listing page
if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
echo '<style type="text/css">
#favorite-actions, .add-new-h2, .tablenav { display:none; }
</style>';
}
}
add_action('admin_menu', 'disable_new_posts');
EDIT: To prevent direct access if someone types the URL in themselves: https://wordpress.stackexchange.com/a/58292/6003
WordPress Networks: I found that Seamus Leahy's answer doesn't work if you are logged in as a super admin of the network, it doesn't matter if the user doesn't have the capability, mapped or otherwise, when current_user_can($cap) is called by the CMS. By digging into the core I found you can do the following.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
),
'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
));
The accepted answer hides the menu item, but the page is still accessible.
In wordpress and for all the post types there is the capability create_posts. This capability is used in several core files :
wp-admin\edit-form-advanced.php
wp-admin\edit.php
wp-admin\includes\post.php
wp-admin\menu.php
wp-admin\post-new.php
wp-admin\press-this.php
wp-includes\admin-bar.php
wp-includes\class-wp-xmlrpc-server.php
wp-includes\post.php
So if you really want to disable this feautere you must do it per role and per post type.
I use the great plugin "User Role Editor" to manage the capabilities per role.
But what about the capability create_posts? Well this capability is not mapped and also create_posts is equal to create_posts so we should fix this and map the capability per post type.
So you can add this piece of code in your functions.php and the you can manage this capability.
function fix_capability_create(){
$post_types = get_post_types( array(),'objects' );
foreach ( $post_types as $post_type ) {
$cap = "create_".$post_type->name;
$post_type->cap->create_posts = $cap;
map_meta_cap( $cap, 1);
}
}
add_action( 'init', 'fix_capability_create',100);
So here we are not hiding or removing menu elements... here we are removing the capability for users (including xmlrpc requests).
The action was init and not admin_init or anything else because init at priority 100 prevents the display of "add new" on admin bar, sidebar, etc (in all the wp interface).
Disable creating new post for registered post-types: (example for post and page)
function disable_create_newpost() {
global $wp_post_types;
$wp_post_types['post']->cap->create_posts = 'do_not_allow';
//$wp_post_types['page']->cap->create_posts = 'do_not_allow';
//$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
}
add_action('init','disable_create_newpost');
add_action("load-post-new.php", 'block_post');
function block_post()
{
if($_GET["post_type"] == "custom_type")
wp_redirect("edit.php?post_type=custom_type");
}
# Staffan Estberg,
This is best way to hide the Add New or Create New button in custom postypes
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
It disable to create new post in custom post types both side in admin menu and above the list of post type.
I found this simplest way for this. Just ad this code into theme’s function.php.
function hd_add_buttons() {
global $pagenow;
if (is_admin()) {
if ($_GET['post_type'] == 'custom_post_type_name') {
echo '<style>.add-new-h2{display: none !important;}</style>';
}
}
}
add_action('admin_head', 'hd_add_buttons');
As the question is 'how to disable add-new button on custom post type', and not 'how to restrict user editing custom post types', in my opinion the answer should be purely hiding the buttons with css, by adding this to the functions.php file :
add_action( 'admin_head', function(){
ob_start(); ?>
<style>
#wp-admin-bar-new-content{
display: none;
}
a.page-title-action{
display: none !important;
}
#menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
display:none;
}
</style>
<?php ob_end_flush();
});