I want to get the post that is assigned to this template and display the content,
currently, I can only display the post content if I put the post id manually. I only want to display the content if the post is assigned to this template and display the content inside woocommerce page
<?php
/**
* Template Name: Sign up Page
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
echo 'this is the signup page';
$post = get_post(2);
$output = apply_filters( 'the_content', $post->post_content );
echo $output;
Updated:
You could try to use get_page_template_slug() function to check, if the current page uses specific template, something like:
while ( have_posts() ) : the_post();
$current_template = get_page_template_slug( get_the_ID() );
if ($current_template == 'signup.php' ) {
the_content();
}
endwhile;
Related
i added following code to use custom template for my recipes single page
function override_single_template( $single_template ){
global $post;
if ($post->post_type == "recipes"){
$single_template = plugins_url('/recipe-single-page-template.php',__FILE__);
}
return $single_template;
}
add_filter( 'single_template', 'override_single_template',10);
and in my template i added following code
<?php
/*
Template Name: recipe-single-page-template
Template Post Type: recipes
*/
require_once("../../../wp-load.php");
?>
<?php get_header(); ?>
<?php echo $post->ID?>
<?php get_footer(); ?>
but i do not access the post and echo out post id will cuses the following error
Trying to get property of non-object
var dump $post outputs null
NULL
and following code will print out my custom template address
$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_url;
top code result:
https://charter.test/wp-content/plugins/recipe-plugin/templates/single-recipes.php
now whate should i do?
Are you trying to redirect a custom post type called recipes to a custom template? You don't need to override, by default wordpress has a built in dynamic content display system.
In your case, upon displaying a recipe it will search first for single-recipe.php, if not found, fallback on single.php, if not found, fallback on 404.php and finally on index.php
You just have to create a file called single-recipe.php.
Your second problem is that there is no loop displayed in your file, you have to tell to wordpress that if a post exist, it should retrieve it and present it to you. For that we use the loop system.
Your single-recipe.php file should look like something like this:
<?php
/**
* Get theme header
*/
get_header();
/**
* Start loop
*/
if ( have_posts() ):
while ( have_posts() ):
the_post();
/**
* If we find a post, output the tilte
*/
echo the_title().'<br/>';
/**
* End loop
*/
endwhile;
endif;
/**
* Get theme footer
*/
get_footer(); ?>
Hi i want to add new custom title and links into My Account page into my WooCommerce site.. Searched all documentation, and Stackoverflow topics, but not found sollution for my request.
This is title for what im asking for
Text Muj Ucet is My Account in english. :)
i want to add new title like is shown in image bellow:
This is WooCommerce Template Code for that part:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<p><?php
/* translators: 1: user display name 2: logout url */
printf(
__( 'Hello %1$s (not %1$s? Log out)', 'woocommerce'
),
'<strong>' . esc_html( $current_user->display_name ) . '</strong>',
esc_url( wc_logout_url( wc_get_page_permalink( 'myaccount' ) ) )
);
?></p>
<p>Na nástěnce svého uživatelského účtu si můžete stáhnout své zakoupené
produkty a faktury, upravit své osobní informace, změnit heslo nebo
fakturační adresu.</p>
<?php
/**
* My Account dashboard.
*
* #since 2.6.0
*/
do_action( 'woocommerce_account_dashboard' );
/**
* Deprecated woocommerce_before_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_before_my_account' );
/**
* Deprecated woocommerce_after_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_after_my_account' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already
sent" issues. */
I want to add new one title bellow that sidebar. How to register a new title ?
Thanks
Here is the solution.
add_filter('woocommerce_account_menu_items', 'display_account_new_link');
function display_account_new_link( $items ) {
$items['new_link'] = __( 'New Link', 'text-domain' );
return $items;
}
add_action( 'woocommerce_account_new_link_endpoint', 'new_account_link_content' );
function new_account_link_content() {
//include your display template here
echo "Here goes you content";
}
After pasting this code in your plugin or in your theme function.php file this code will make a new link in the my account navigation sidebar, along with the template you want to assign this link. Here new_link is the slug for this navigation link. If you want to give some different slug you must rename new_link written everywhere in the given code. As soon as you click on this New Link it will redirect you to the Page Not Found Page. It can be solved by adding this code.
add_action( 'init', 'register_new_link_endpoint');
function register_new_link_endpoint() {
add_rewrite_endpoint( 'new_link', EP_PAGES );
}
After pasting this code you must save your permalink once, by going to WordPress Dashboard->Settings->Permalinks and hit the save changes button.
I am trying to overwrite the <title> tag of a page using a Wordpress plugin.
I don't want to change the theme's code. I just wanna force the theme to change some page titles via the plugin.
The theme uses add_theme_support( 'title-tag' ). Note that the use of wp_title is now deprecated.
Your problem is that you can not use wp_title() in the theme if the theme already supports title-tag. The <head> of your theme should look like this:
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php wp_head(); ?>
</head>
The filter and title-tag support:
add_action( 'after_setup_theme', 'my_theme_functions' );
function my_theme_functions() {
add_theme_support( 'title-tag' );
}
add_filter( 'wp_title', 'custom_titles', 10, 2 );
function custom_titles( $title, $sep ) {
//set custom title here
$title = "Some other title" . $title;;
return $title;
}
If you do this, it will work perfectly.
I posted this answer for another question but since it is relevant and more up-to-date, I though it might be useful for some people.
How document title is generated has changed since Wordpress v4.4.0. Now wp_get_document_title dictates how title is generated:
/**
* Displays title tag with content.
*
* #ignore
* #since 4.1.0
* #since 4.4.0 Improved title output replaced `wp_title()`.
* #access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
Here is the code from v5.4.2. Here are the filters you can use to manipulate title tag:
function wp_get_document_title() {
/**
* Filters the document title before it is generated.
*
* Passing a non-empty value will short-circuit wp_get_document_title(),
* returning that value instead.
*
* #since 4.4.0
*
* #param string $title The document title. Default empty string.
*/
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
return $title;
}
// --- snipped ---
/**
* Filters the separator for the document title.
*
* #since 4.4.0
*
* #param string $sep Document title separator. Default '-'.
*/
$sep = apply_filters( 'document_title_separator', '-' );
/**
* Filters the parts of the document title.
*
* #since 4.4.0
*
* #param array $title {
* The document title parts.
*
* #type string $title Title of the viewed page.
* #type string $page Optional. Page number if paginated.
* #type string $tagline Optional. Site description when on home page.
* #type string $site Optional. Site title when not on home page.
* }
*/
$title = apply_filters( 'document_title_parts', $title );
// --- snipped ---
return $title;
}
So here are two ways you can do it.
First one uses pre_get_document_title filter which short-circuits the title generation and hence more performant if you are not going make changes on current title:
function custom_document_title( $title ) {
return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
Second way uses document_title_separator and document_title_parts hooks for the title and the title seperator that are executed later in the function, after title is generated using functions like single_term_title or post_type_archive_title depending on the page and about to be outputted:
// Custom function should return a string
function custom_seperator( $sep ) {
return '>';
}
add_filter( 'document_title_separator', 'custom_seperator', 10 );
// Custom function should return an array
function custom_html_title( $title ) {
return array(
'title' => 'Custom Title',
'site' => 'Custom Site'
);
}
add_filter( 'document_title_parts', 'custom_html_title', 10 );
Context: WordPress 5.4.5, Yoast 3.7.1
I'm a plugin developer who has access to the client's site. The site has Yoast 3.7.1 installed and I'm wondering if that is significant because no matter what I do I can't change the 404 page's title.
Now on other pages on StackOverflow where similar questions have been posed (here, here and here for example), those answering have asked if the header.php is correctly embedding a call to wp_title(). Here's what's in the current theme's header.php at that point:
<title><?php wp_title( '|', true, 'right' ); ?></title>
Interestingly, in my 404.php page, wp_get_document_title() tells me that the document title is Page not found - XXXX even though the wp_title call above specifies the separator as |. Yoast's rewriting of titles has been disabled so I'm not at all sure where that dash is coming from.
My plugin does a REST call and pulls in content from off-site for inclusion in the page. Part of that content is the text to be used in the title.
On previous client sites, I've been able to do the following:
add_filter('wp_title', 'change_404_title');
function change_404_title($title) {
if (is_404())
{
global $plugin_title;
if (!empty($plugin_title))
{
$title = $plugin_title;
}
}
return $title;
}
However, on this site, that's not working.
I have tried, based on the version of WordPress being used, hooking the pre_get_document_title filter, viz
add_filter('pre_get_document_title', 'change_404_title');
but again to no avail. I am currently reading up on Yoast ...
wp_title deprecated since version 4.4. So we should use the new filter pre_get_document_title. Your code looks fine but I am confused about global $plugin_title. I would rather ask you to Try this first
add_filter('pre_get_document_title', 'change_404_title');
function change_404_title($title) {
if (is_404()) {
return 'My Custom Title';
}
return $title;
}
If it doesn't work then try changing the priority to execute your function lately.
add_filter('pre_get_document_title', 'change_404_title', 50);
How document title is generated has changed since Wordpress v4.4.0. Now wp_get_document_title dictates how title is generated:
/**
* Displays title tag with content.
*
* #ignore
* #since 4.1.0
* #since 4.4.0 Improved title output replaced `wp_title()`.
* #access private
*/
function _wp_render_title_tag() {
if ( ! current_theme_supports( 'title-tag' ) ) {
return;
}
echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}
Here is the code from v5.4.2. These are the filters you can use to manipulate title tag:
function wp_get_document_title() {
/**
* Filters the document title before it is generated.
*
* Passing a non-empty value will short-circuit wp_get_document_title(),
* returning that value instead.
*
* #since 4.4.0
*
* #param string $title The document title. Default empty string.
*/
$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
return $title;
}
// --- snipped ---
/**
* Filters the separator for the document title.
*
* #since 4.4.0
*
* #param string $sep Document title separator. Default '-'.
*/
$sep = apply_filters( 'document_title_separator', '-' );
/**
* Filters the parts of the document title.
*
* #since 4.4.0
*
* #param array $title {
* The document title parts.
*
* #type string $title Title of the viewed page.
* #type string $page Optional. Page number if paginated.
* #type string $tagline Optional. Site description when on home page.
* #type string $site Optional. Site title when not on home page.
* }
*/
$title = apply_filters( 'document_title_parts', $title );
// --- snipped ---
return $title;
}
So here are two ways you can do it.
First one uses pre_get_document_title filter which short-circuits the title generation and hence more performant if you are not going make changes on current title:
function custom_document_title( $title ) {
return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
Second way uses document_title_separator and document_title_parts hooks for the title and the title seperator that are executed later in the function, after title is generated using functions like single_term_title or post_type_archive_title depending on the page, and just before the title tags is about to be outputted:
// Custom function should return a string
function custom_seperator( $sep ) {
return '>';
}
add_filter( 'document_title_separator', 'custom_seperator', 10 );
// Custom function should return an array
function custom_html_title( $title ) {
return array(
'title' => 'Custom Title',
'site' => 'Custom Site'
);
}
add_filter( 'document_title_parts', 'custom_html_title', 10 );
Add this to your functions.php
function custom_wp_title($title) {
if ( is_404() ) {
$title = 'Custom 404 Title';
}
return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );
10 - is priority change to overwrite other plugins like SEO
I am using KLEO theme for my work.
I am trying to filter posts by category when the date archive is called.
I did this by modifying the core files of the theme. However when the code is run it goes into infinite loop and definitely doesn't produce the results required.
e.g. if I go to localhost/wordpress/2014/06?category=events it should show posts on that date that are only from this category. However it goes into the infinite loop.
The modified code is here:
<?php
/**
* The template for displaying Archive pages
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you'd like to further customize these archive views, you may create a
* new template file for each specific one. For example, Twenty Fourteen
* already has tag.php for Tag archives, category.php for Category archives,
* and author.php for Author archives.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Kleo
* #since Kleo 1.0
*/
get_header(); ?>
<?php
//Specific class for post listing */
$blog_type = sq_option('blog_type','masonry');
$template_classes = $blog_type . '-listing';
if ($blog_type == 'standard' && sq_option('blog_meta_status', 1) == 1) { $template_classes .= ' with-meta'; }
add_filter('kleo_main_template_classes', create_function('$cls','$cls .=" posts-listing '.$template_classes.'"; return $cls;'));
?>
<?php get_template_part('page-parts/general-title-section'); ?>
<?php get_template_part('page-parts/general-before-wrap'); ?>
<?php if ( have_posts() ) : ?>
<?php
if ($blog_type == 'masonry') {
echo '<div class="row">'
.'<div class="grid-posts kleo-isotope masonry">';
}
?>
<?php
function the_loop($the_query){
// Start the Loop.
while ( $the_query ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
if ($blog_type == 'masonry') {
get_template_part( 'page-parts/post-content-masonry');
}
else {
get_template_part( 'content', get_post_format() );
}
endwhile;
}
if( is_date() ){
if(isset($_GET["category"])){
$category_slug = trim($_GET["category"]);
$args=array('category_name = '.$category_slug.'');
$the_query = new WP_Query( $args );
$lol =$the_query->have_posts();
the_loop($lol);
}
}
else{
$the_query=have_posts();
the_loop($the_query);
}
?>
<?php
if ($blog_type == 'masonry') {
echo '</div>'
.'</div>';
}
?>
<?php
// page navigation.
kleo_pagination();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
<?php get_template_part('page-parts/general-after-wrap'); ?>
<?php get_footer(); ?>
Please help!!!