Wordpress, build a template for a post - wordpress

I would like to know how can I define a post template and adding automatically the html layout when we are creating a post?
Do I have to use custom fields?
For example:
Post content:
Title
Description 1
Description 2
With links and social media
Html layout result:
<h1 class="work-title">Title</h1>
<div class="description">Description 1</div>
<div class="description">Description 2</div>
<div class="details">With links and social media</div>
</div>
Thanks

You could alter the output of the function the_content() by using (in functions.php) something like:
function alter_content($content) {
// Alter the $content variable here
return $content;
}
add_filter( 'the_content', 'alter_content', 6);
Which will alter the content before the paragraphs are added. And if you don't want paragraphs at all use:
remove_filter('the_content', 'wpautop');
Check out the codex page on apply_filters() for more info on the function.

Related

Wordpress outer shortcode parsing html tag of inner shortcode's content

I'm going to write some example code, so it's a shortened example of the issue I'm experiencing.
Let's say I have the following text stored in the database:
[form]
<ul>
[each name="upgrades"]
<li><input type="checkbox" [value name="upgrade_name" id="1"] />[value name="upgrade_name" id="2"]</li>
[/each]
</ul>
[/form]
If I run do_shortcode on this text, the shortcodes INSIDE html tags INSIDE the each content is parsed instead of being deferred to the each shortcode. However, shortcodes that are not in html tags in the each content are not parsed until the each shortcode runs do_shortcode on it's content, which should be the correct behavior.
In otherwords, the value shortcode with id 1 is parsed too soon (on the form shortcode pass), but the value shortcode with id 2 is not parsed until the each shortcode runs do_shortcode on it, so it produces the correct value.
I know I can set the ignore_html flag on the form shortcode to true, but that is incorrect as a user may want to have html tags parsed for shortcodes.
Is there a workaround for this behavior?
Wordpress version 4.6.1
EDIT: Adding reproducible code
Create a new plugin with this code:
<?php
/*
Plugin Name: Broken Shortcodes
Description: Shortcodes should not jump the gun in parsing html tag shortcodes of inner shortcode content.
*/
remove_filter('the_content', 'wpautop');
add_shortcode('form', function($atts, $content){
echo "<textarea>This is the form's content:\n".$content.'</textarea>';
return "<textarea>This is the rendered form shortcode:\n".do_shortcode($content).'</textarea>';
});
$bad_global_variable = 'first';
add_shortcode('value', function($atts, $content){
global $bad_global_variable;
return $bad_global_variable;
});
add_shortcode('each', function($atts, $content){
global $bad_global_variable;
$_content = '';
foreach(array('second', 'third', 'fourth') as $v){
$bad_global_variable = $v;
$_content .= do_shortcode($content);
}
return $_content;
});
?>
Create a page with this text:
[form]
[each]
<div [value]>[value]</div>
[/each]
[/form]
The output is incorrect:
<div first>second</div>
<div first>third</div>
<div first>second</div>
<div first>fourth</div>
<div first>second</div>
<div first>third</div>
<div first>second</div>
A quick workaround is to parse your own shortcodes out of html tags.
So you text would look like:
[form]
[each]
<!-- Notice curly brackets -->
<div {value}>[value]</div>
[/each]
[/form]
Then your each shortcode might look like this:
add_shortcode('each', function($atts, $content){
global $bad_global_variable;
$content = preg_replace('/\{([^}]+)\}/', '[$1]', $content, -1);
$_content = '';
add_filter( 'wp_kses_allowed_html', 'parse_tags', 10, 2 );
foreach(array('second', 'third', 'fourth') as $v){
$bad_global_variable = $v;
$_content .= do_shortcode($content);
}
remove_filter( 'wp_kses_allowed_html', 'parse_tags', 10, 2 );
return $_content;
});
// This might be necessary depending on your use-case
function parse_tags($tags, $context){
$tags['input']['value'] = true;
return $tags;
}
But, this should not be a solution to something seemingly so simple. I think the Shortcode API needs some TLC.
See ticket here: https://core.trac.wordpress.org/ticket/33134

wordpress apply_filter values are not coming inline

I am using follwing code in my wordpress custom theme template
<p id="news"> <?php echo apply_filters('the_content',$myNews)?></p>;
And the desired output should be like this
<p id="news"> Herer goes my news content from $myNewsvariable </p>
But i am getting output like this
<p id="news"></p>Here goes my news content from $myNewsvariable
Please tell me how i can fix this
the_content function prints out content by default. So there is no need to do a duplicate echo before apply_filters.
Also you can apply your filter to get_the_content:
<?php echo apply_filters('get_the_content', $myNews); ?>

Custom Navigation and Header Area in Genesis Framework

I'm trying to customize the navigation and header region of my Genesis 2 theme. Here is the code I'm using:
/* Reposition the primary navigation menu
remove_action( 'genesis_after_header', 'genesis_do_nav' );
function new_header(){ ?>
<div id="title-area">
<div id="new-title">
<h1>new Title</h1>
</div>
<div id="new-nav">
<nav class="nav-primary" role="navigation" itemscope="itemscope" itemtype="http://schema.org/SiteNavigationElement"><div class="wrap">
<?php
add_action( 'genesis_do_nav' );
?></div>
</nav>
</div>
<div id="new-search">
<p>Search Box will need to go in here</p>
</div>
</div>
<?php
}
add_action( 'genesis_before_header', 'new_header' );
The title is printing out fine, and the paragraph where I will want to add my search box is printing fine too. HOwever, for some reason I am not getting any output for my menu.
What am I missing? THere is nothing being printed at all when I inspect the element.
Thanks in advance.
Couldn't you alter the CSS of each of these elements the default way Genesis provides it? The only thing I see that is different from the default way is that you want a search box underneath the navigation.
If that's the case, then just add the function for the search box and position it after the nav. For example check below
add_action('genesis_after_header', 'move_my_nav');
function move_my_nav() {
echo '<div id="new-search">';
echo '<p>Search Box will need to go in here</p>';
echo '</div>';
}

Wordpress comments without depth

I convert a HTML template to wordpress theme and I've a trouble.
I just want to add some right padding to reply comments.
The wp_list_comments() function add child comments into a div with parent comment.
Wordpress function output:
<div>
<div class="comment-1"> // comment 1
<div class="comment-2"></div> // reply 1
<div class="comment-3"></div> // reply 2
</div>
<div class="comment-4"> // comment 2
</div>
What I need for my template is:
<div>
<div class="comment-1"></div> // comment 1
<div class="comment-2" class="reply"></div> // reply 1
<div class="comment-3" class="reply"></div> // reply 2
<div class="comment-4"> // comment 2
</div>
You need to customise the comments loop using comments.php in your theme. The code is somewhat extensive requiring use of your functions.php file.
Check out this WP Tuts article on making use of the files to override the default output.

How can i display permalink inside the excerpt paragraphs

im trying to show the permalink right inside/after the excerpt
the code that im using at a designated area in my theme is this
<div id="headline">
<?php the_excerpt(); echo ''; echo '[Read more...]'; echo '';?>
</div>
the result
<p>the excerpt here</p>
<a>permalink here</a>
how can i show the permalink inside the same paragraphs that the excerpt is displayed ?
<p> the_excerpt; <a>permalink</a> </p>
thanks all.
You can place the following code in the functions.php fule of your theme
function new_excerpt_more($more) {
global $post;
return '[Read more...]';
}
add_filter('excerpt_more', 'new_excerpt_more');
This codex entry has more details on how you can modify what the excerpt spits out : http://codex.wordpress.org/Function_Reference/the_excerpt
I tried the above solution but it didn't solve my problem, the A tag still showed outside the P tags. I solved my problem using get_the_excerpt(), which returned just the excerpt text without the P tags.
http://codex.wordpress.org/Function_Reference/get_the_excerpt

Resources